package com.centaline.androidsalesblog.activities.search;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import com.android.volley.VolleyError;
import com.centaline.androidsalesblog.activities.newest.NewEstDetailActivity;
import com.centaline.androidsalesblog.api.newest.NewEstListApi;
import com.centaline.androidsalesblog.base.BaseSearchActivity;
import com.centaline.androidsalesblog.constants.AppConstant;
import com.centaline.androidsalesblog.constants.DefaultHint;
import com.centaline.androidsalesblog.constants.ExtraConstant;
import com.centaline.androidsalesblog.model.databaseobject.NewEstDo;
import com.centaline.androidsalesblog.model.databaseobject.SearchDo;
import com.centaline.androidsalesblog.model.responseobject.NewEstListRo;
import com.centanet.framework.interfaces.IResponseListener;

import java.util.ArrayList;
import java.util.Locale;

/**
 * 新房搜索
 * Created by guilin on 16/5/4.
 */
public class NewEstateSearchActivity extends BaseSearchActivity {

    private NewEstListApi newEstListApi;

    @Override
    protected void initViews() {
        super.initViews();
        newEstListApi = new NewEstListApi(NewEstateSearchActivity.class, NewEstListRo.class,
                new IResponseListener<NewEstListRo>() {
                    @Override
                    public void onResponse(NewEstListRo newEstListRo) {
                        ArrayList<NewEstDo> temp = newEstListRo.getNewEstDoArrayList();
                        searchList.clear();
                        if (temp == null || temp.size() == 0) {
                            if (fromUser) {
                                snack("搜索结果为空");
                            } else {
                                rv_search.setVisibility(View.GONE);
                                showHistoryLayout(true);
                            }
                        } else {
                            SearchDo searchDo;
                            for (NewEstDo newEstDo : temp) {
                                searchDo = new SearchDo(type, AppConstant.CityCode);
                                searchDo.setTitle(String.format(Locale.CHINA, "%s【%s】",
                                        newEstDo.getAdName(), newEstDo.getEstType()));
                                searchDo.setCode(newEstDo.getEstExtId());
                                searchList.add(searchDo);
                            }
                            searchAdapter.notifyDataSetChanged();
                            rv_search.setVisibility(View.VISIBLE);
                            showHistoryLayout(false);
                        }
                    }

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        if (fromUser)
                            netWorkUnable();
                    }
                });
    }

    @Override
    protected DefaultHint getDefaultHint() {
        type = 10;
        getHistory(true);
        return DefaultHint.NEWESTATE;
    }

    @Override
    protected void onItemClick(SearchDo searchDo, boolean insert) {
        super.onItemClick(searchDo, insert);
        if ("key".equals(searchDo.getCategory())) {
            Intent intent = new Intent();
            intent.putExtra(ExtraConstant.SearchContent, searchDo.getTitle());
            setResult(RESULT_OK, intent);
            finish();
        } else {
            startActivity(new Intent(this, NewEstDetailActivity.class)
                    .putExtra(ExtraConstant.NewEstId, searchDo.getCode()));
        }
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        super.onQueryTextSubmit(query);
        fromUser = true;
        if (TextUtils.isEmpty(query))
            return true;
        Intent intent = new Intent();
        intent.putExtra(ExtraConstant.SearchContent, query);
        setResult(RESULT_OK, intent);
        finish();
        return true;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        fromUser = false;
        if (TextUtils.isEmpty(newText)) {
            rv_search.setVisibility(View.GONE);
            showHistoryLayout(true);
        } else {
            newEstListApi.setTitle(newText);
            newEstListApi.startRequest();
        }
        return true;
    }
}
